package comits.web.client.servlets.gestion.drp;

import java.awt.Color;
import java.awt.GradientPaint;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;

import javax.servlet.http.HttpSession;
 
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.chart.renderer.xy.XYSplineRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.time.Day;
import org.jfree.data.time.Month;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.time.Year;
import org.jfree.data.xy.DefaultXYDataset;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.RectangleInsets;

import comits.business.tiers.dao.PrevisionDAO;
import comits.business.tiers.dao.PrevisionDAOImpl;
import comits.business.tiers.dao.ProduitDAO;
import comits.business.tiers.dao.ProduitDAOImpl;
import comits.business.tiers.metiers.Prevision;
import comits.business.tiers.utilitaires.Dates;
 
public class BarChart
{
    private static XYDataset createDataset(int btqN)
    {
    	TimeSeries xy = null;
    	TimeSeriesCollection xyseriescollection = new TimeSeriesCollection();
    	Hashtable<String, TimeSeries>  var=new Hashtable<String, TimeSeries>();
    	String serie ="";
    	Dates dats = new Dates();
    	//boolean suit =false;
    	int n=0;
    	PrevisionDAO pDao = new PrevisionDAOImpl();
    	ProduitDAO pdtDao = new ProduitDAOImpl();
    	List<Prevision> lPrev = pDao.getListPrevGraph(btqN);
    	int pdt, oldPdt = 0;
		for(Prevision p : lPrev){
			pdt= p.getNumPdt();
			if(oldPdt != pdt){
				oldPdt = pdt;
				serie = pdtDao.recupereProduit(pdt).getLibelle();
				var.put(serie, new TimeSeries(serie,Month.class));
				n=0;
				xy=null;
			}
			
			n++;
			xy = (TimeSeries)var.get(serie);
			//xy.(p.getDatePrev(),p.getQuantite());
			xy.add(new Month(dats.moisInt(p.getPeriode())+1,
					p.getAnnee()),p.getQuantite());
			/*if (n==2) xy.add(2D,p.getQuantite());
			if (n==3) xy.add(3D,p.getQuantite());
			if (n==4) xy.add(4D,p.getQuantite());
			if (n==5) xy.add(5D,p.getQuantite());
			if (n==6) xy.add(6D,p.getQuantite());
			if (n==7) xy.add(7D,p.getQuantite());
			if (n==8) xy.add(8D,p.getQuantite());
			if (n==9) xy.add(9D,p.getQuantite());
			if (n==10) xy.add(10D,p.getQuantite());
			if (n==11) xy.add(11D,p.getQuantite());
			if (n==12) xy.add(12D,p.getQuantite());*/
			if (n==19) xyseriescollection.addSeries(xy);
			
		}
    	
    	
    /*	NumPrevision	NumSite	NumPdt	Periode	Quantite	Site	Annee
    	1	1	153	Janvier		Boutique	2012
    	2	1	153	Fevrier		Boutique	2012
    	3	1	153	Mars		Boutique	2012
    	4	1	153	Avril		Boutique	2012
    	5	1	153	Mai		Boutique	2012
    	6	1	127	Janvier	23	Boutique	2012
    	7	1	127	Fevrier		Boutique	2012
    	8	1	127	Mars		Boutique	2012
    	9	1	127	Avril		Boutique	2012
    	10	1	127	Mai		Boutique	2012
    	11	1	128	Janvier	26	Boutique	2012
    	12	1	128	Fevrier	12	Boutique	2012
    	13	1	128	Mars	25	Boutique	2012
    	14	1	128	Avril	27	Boutique	2012
    	15	1	128	Mai	38	Boutique	2012
    	121	1	153	Janvier		Boutique	2011
    	122	1	153	Fevrier		Boutique	2011
    	123	1	153	Mars		Boutique	2011
    	124	1	153	Avril		Boutique	2011
    	125	1	153	Mai		Boutique	2011
    	133	1	127	Janvier		Boutique	2011
    	134	1	127	Fevrier		Boutique	2011
    	135	1	127	Mars		Boutique	2011
    	136	1	127	Avril	25	Boutique	2011
    	137	1	127	Mai		Boutique	2011
    	145	1	128	Janvier	50	Boutique	2011
    	146	1	128	Fevrier	25	Boutique	2011
    	147	1	128	Mars	35	Boutique	2011
    	148	1	128	Avril	40	Boutique	2011
    	149	1	128	Mai	49	Boutique	2011*/
		
		/*xyseries.add(JanvierD, 183);
		xyseries.add(2D, 183);
		xyseries.add(3D, 185);
		xyseries.add(4D, 144);
		xyseries.add(5D, 181);
		xyseries.add(6D, 143);
		xyseries.add(7D, 212);
		xyseries.add(8D, 226);
		xyseries.add(9D, 138);
		xyseries.add(10D, 189);
		xyseries.add(11D, 158);
		XYSeries xyseries1 = new XYSeries("Produit 127");
		xyseries1.add(7D, 23);
		xyseries1.add(8D, 25);
		xyseries1.add(9D, 44);
		xyseries1.add(10D, 32);
		xyseries1.add(11D,38);
		xyseries1.add(6D, 14);
		xyseries1.add(5D, 15);
		xyseries1.add(4D, 42);
		xyseries1.add(3D, 42);
		xyseries1.add(2D, 29);*/
		xyseriescollection.setDomainIsPointsInTime(true);
		return xyseriescollection;
      /* 
        DefaultXYDataset cdataset = new DefaultXYDataset();
        cdataset.addValue(100, "Prevision", "Janvier 2011");
        cdataset.addValue(150, "Prevision", "Fevrier 2011");
        cdataset.addValue(80, "Prevision", "Mars 2011");
        cdataset.addValue(50, "Prevision", "Avril 2011");
        cdataset.addValue(250, "Prevision", "Mai 2011");
        cdataset.addValue(175, "Prevision", "Juin 2011");
        cdataset.addValue(201, "Prevision", "Juillet 2011");
        cdataset.addValue(300, "Prevision", "Aout 2011");
        cdataset.addValue(52, "Prevision", "Septembre 2011");
        cdataset.addValue(150, "Prevision", "Novembre 2011");
        cdataset.addValue(130, "Prevision", "Decembre 2011");
        cdataset.addValue(230, "Prevision", "Janvier 2012");
        return cdataset;*/
    }
    public static JFreeChart createChart(int btqN,String typeChart,String boutique)
    {
    	XYDataset categorydataset = createDataset(btqN);
    	DateAxis numberaxis = new DateAxis("X");
		//numberaxis.setAutoRange();
		numberaxis.setDateFormatOverride(new SimpleDateFormat("MMM-yyyy"));
		NumberAxis numberaxis1 = new NumberAxis("Y");
		//numberaxis1.setAutoRangeIncludesZero(false);
		//XYSplineRenderer xysplinerenderer = new XYSplineRenderer();
		//xyplot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
		//JFreeChart jfreechart = new JFreeChart("XYSplineRenderer", JFreeChart.DEFAULT_TITLE_FONT, xyplot, true);
		JFreeChart jfreechart = null ;
		if(typeChart.equalsIgnoreCase("curve")) jfreechart = ChartFactory.createTimeSeriesChart("Visualisation des previsions - " + boutique, "Mois", "Quantite", categorydataset, true, true, false);
		if(typeChart.equalsIgnoreCase("bar")) jfreechart = ChartFactory.createBarChart("Visualisation des previsions - " + boutique, "Mois", "Quantite", (CategoryDataset) categorydataset, PlotOrientation.VERTICAL, true, true, false);
		XYPlot xyplot = (XYPlot) jfreechart.getPlot();//new XYPlot(categorydataset, numberaxis, numberaxis1, xysplinerenderer);
		xyplot.setBackgroundPaint(Color.lightGray);
		xyplot.setDomainGridlinePaint(Color.white);
		xyplot.setRangeGridlinePaint(Color.white);
		xyplot.setDomainCrosshairVisible(true);
	    xyplot.setRangeCrosshairVisible(true);
		jfreechart.setBackgroundPaint(Color.white);
		
		 XYItemRenderer r = xyplot.getRenderer();
	        if (r instanceof XYLineAndShapeRenderer) {
	            XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) r;
	            renderer.setBaseShapesVisible(true);
	            renderer.setBaseShapesFilled(true);
	        }
		
	return jfreechart;
		
        /*CategoryDataset categorydataset = createDataset();
 
        JFreeChart jfreechart = ChartFactory.createBarChart("Visualisation des previsions", "Mois", "Quantite", categorydataset, PlotOrientation.VERTICAL, true, true, false);
        jfreechart.setBackgroundPaint(Color.white);
        XYPlot categoryplot = (XYPlot)jfreechart.getPlot();
        categoryplot.setBackgroundPaint(Color.lightGray);
        categoryplot.setDomainGridlinePaint(Color.white);
        categoryplot.setDomainGridlinesVisible(true);
        categoryplot.setRangeGridlinePaint(Color.white);
        NumberAxis numberaxis = (NumberAxis)categoryplot.getRangeAxis();
        numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
        XYItemRenderer barrenderer = (XYItemRenderer) categoryplot.getRenderer();
       // BarRenderer barrenderer = (BarRenderer)categoryplot.getRenderer();
      //  barrenderer.setDrawBarOutline(false);
        Float h,s,b;
        h=(float) 0.375;
        s=(float) 0.711;
        b= (float) 0.882;
        GradientPaint gradientpaint = new GradientPaint(0.0F, 0.0F, Color.getHSBColor(h,s,b), 0.0F, 0.0F, new Color(0, 0, 64));
        //GradientPaint gradientpaint1 = new GradientPaint(0.0F, 0.0F, Color.green, 0.0F, 0.0F, new Color(0, 64, 0));
        //GradientPaint gradientpaint2 = new GradientPaint(0.0F, 0.0F, Color.red, 0.0F, 0.0F, new Color(64, 0, 0));
        barrenderer.setSeriesPaint(0, gradientpaint);
        //barrenderer.setSeriesPaint(1, gradientpaint1);
        //barrenderer.setSeriesPaint(2, gradientpaint2);
        ValueAxis categoryaxis = categoryplot.getDomainAxis();
      //  categoryaxis.setL
      //  categoryaxis.setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(0.52359877559829882D));
        return jfreechart;*/
    }
}
